// pages/mine/mine.js

var WxParse = require("../../wxParse/wxParse.js");
var api = require("../../api.js"),
  e = require("../../utils/util.js"),
  r = "",
  tips = '',
  app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: "",
    data: true,
    type: '2',
    upload_picture_list: []
  },
  onLoad: function (options) {
    r = this;
    /** 检测是否授权 */
    getApp().is_empower();

    // var openid = app.globalData.wxUserInfo.openId;
    if (options.type) {
      r.setData({
        type: options.type,
        // openid:openid
      });
    }
  },
  onShow: function () {
    var _this = this;
    r.toast = _this.selectComponent("#toast");
    tips = _this.selectComponent("#mytips");
  },
  //选择图片方法
  uploadpic: function (e) {
    var that = this; //获取上下文
    var upload_picture_list = that.data.upload_picture_list;
    let maxSize = 1024 * 1024;
    let maxLength = 1;
    let flag = true;
    //选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function (res) {
        wx.showToast({
          title: "正在上传",
          icon: "loading",
          mask: true,
          duration: 500
        });

        var tempFiles = res.tempFiles;
        //把选择的图片 添加到集合里
        for (var i in tempFiles) {
          if (res.tempFiles[i].size > maxSize) {
            flag = false;
            wx.showModal({
              content: "图片太大，不允许上传",
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log("用户点击确定");
                }
              }
            });
          }
          if (res.tempFiles[i].length > maxLength) {
            wx.showModal({
              content: "最多能上传" + maxLength + "张图片",
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log("用户点击确定");
                }
              }
            });
          }
          if (flag == true && res.tempFiles.length <= maxLength) {
            tempFiles[i]["upload_percent"] = 0;
            tempFiles[i]["path_server"] = "";
            upload_picture_list.push(tempFiles[i]);
            //显示
            that.setData({
              upload_picture_list: upload_picture_list
            });
            that.uploadimage(); //调用上传方法
          }
        }
      }
    });
  },
  //点击上传事件
  uploadimage: function () {
    var page = this;
    var upload_picture_list = page.data.upload_picture_list;
    //循环把图片上传到服务器 并显示进度
    for (var j in upload_picture_list) {
      console.log(upload_picture_list[j]);
      if (upload_picture_list[j]["upload_percent"] == 0) {
        //调用函数
        e.upload_file_server(
          "https://duilu.showtimetech.com/duilu/common/wxUploadPic",
          page,
          upload_picture_list,
          j
        );
        console.log(page.data);
      }
    }
  },
  // 删除图片
  deleteImg: function (e) {
    let upload_picture_list = this.data.upload_picture_list;
    let index = e.currentTarget.dataset.index;
    upload_picture_list.splice(index, 1);
    this.setData({
      upload_picture_list: upload_picture_list
    });
  },
  /** 提交申请 */
  submitForm: function (e) {
    var user = getApp().globalData.wxUserInfo;
    var _that = this;
    var t = e.detail.value;
    var p = new Object();
    var open_id = getApp().globalData.open_id;

    if (t.copName == '' || t.copName == null) {
      tips.showTopTip('公司名称不能为空');
      return;
    };
    if (t.copPhone == '' || t.copPhone == null) {
      tips.showTopTip('公司名称不能为空');
      return;
    };
    p.copName = t.copName, p.copPhone = t.copPhone;
    if (_that.data.type == 2) {
      if (t.companyName == '' || t.companyName == null) {
        tips.showTopTip('公司名称为空');
        return;
      }
      // if(t.copIdnum ==''|| t.copIdnum==null){
      //   tips.showTopTip('营业执照编号为空');
      //   return;
      // }
      // if(_that.data.upload_picture_list.length<=0){
      //   tips.showTopTip('图片不能为空');
      //   return;
      // };
      // p.copIdnum = t.copIdnum;
      p.companyName = t.companyName,
        // p.certificateImg= _that.data.upload_picture_list[0].path_server,
        p.openId = open_id;
      wx.request({
        url: api.mine.applyToCopartner,
        data: p,
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          if (res.statusCode == 200) {
            if (res.data.code == 200) {
              wx.showToast({
                title: '提交成功',
                icon: 'success',
                duration: 1000,
                mask: true
              });
              setTimeout(() => {
                wx.navigateTo({
                  url: '../result/result',
                });
              }, 1500);
            } else {

              r.toast.showToast(res.data.msg);
            }


          }
        }
      });

    } else {
      if (t.copIdnum1 == '' || t.copIdnum1 == null) {
        tips.showTopTip('身份证号为空');
        return;
      };
      if (t.channelPrice == '' || t.channelPrice == null) {
        tips.showTopTip('渠道价格为空');
        return;
      }
      //  p.copIdnum = t.copIdnum1,
      p.channelPrice = t.channelPrice,
        p.partnerOpenid = open_id;
      wx.request({
        url: api.mine.addSubordinateCopartner,
        data: p,
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          if (res.statusCode == 200) {
            if (res.data.code == 200) {
              wx.showToast({
                title: '添加成功',
                icon: 'success',
                duration: 1000,
                mask: true
              });
              setTimeout(() => {
                var pages = getCurrentPages();//获取页面栈
                var prePage = pages[pages.length - 2];
                //调用上一个页面的onShow方法
                prePage.onLoad();
                wx.navigateBack({
                  delta: 1
                });
              }, 500);
            } else {
              r.toast.showToast(res.data.msg);
            }


          }
        }
      });
    }
  },
  /** 用户点击右上角分享 */
  onShareAppMessage: function (res) {
    return {
      title: '诚邀认证合伙人',
      path: `pages/parner/parner`,
      success: function (res) { },
      fail: function (res) { }
    }
  },

});
